function withData(param) {
	return param < 10 ? '0' + param : '' + param;
}

function getLoopArray(start, end, dtype) {
	var start = start || 0;
	var end = end || 1;
	var array = [];
	for (var i = start; i <= end; i++) {
		if (dtype) {
			array.push(withData(i) + dtype);
		} else {
			array.push(withData(i));
		}

	}
	return array;
}

function getDayArray(start, end, year, month) {
	var newDate = new Date();

	var start = start || 0;
	var end = end || 1;
	var array = [];
	for (var i = start; i <= end; i++) {

		array.push(withData(i) + '日' + getWeekday(year, month, withData(i)));
	}
	return array;
}

function getWeekday(year, month, day) {
	var dt = new Date(year + '/' + month + '/' + day);
	var a = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'];
	return a[dt.getDay()];
}

function getMonthDay(year, month) {

	year = year.substr(0, year.length - 1);
	month = month.substr(0, month.length - 1);
	//console.log(year);
	//console.log(month);
	var flag = year % 400 == 0 || (year % 4 == 0 && year % 100 != 0),
		array = null;

	switch (month) {
		case '01':
		case '03':
		case '05':
		case '07':
		case '08':
		case '10':
		case '12':
			array = getDayArray(1, 31, year, month)
			break;
		case '04':
		case '06':
		case '09':
		case '11':
			array = getDayArray(1, 30, year, month)
			break;
		case '02':
			array = flag ? getDayArray(1, 29, year, month) : getDayArray(1, 28, year, month)
			break;
		default:
			array = '月份格式不正确，请重新输入！'
	}
	return array;
}

function getNewDateArry() {
	// 当前时间的处理
	var newDate = new Date();
	var year = withData(newDate.getFullYear()) + '年',
		mont = withData(newDate.getMonth() + 1) + '月',
		date = withData(newDate.getDate()) + '日',
		hour = withData(newDate.getHours()),
		minu = withData(newDate.getMinutes())
	return [year, mont, date, hour, minu];
}
//获取今天的日期  yyyy-MM-dd
function getNowDate(date) {
	// 当前时间的处理
	// var newDate = new Date();
	var yyyy = withData(date.getFullYear()),
		MM = withData(date.getMonth() + 1),
		dd = withData(date.getDate()),
		d = yyyy + "-" + MM + "-" + dd
	return d;
}

//获取今天的日期  yyyy-MM-dd
function getNewDate(date) {
	// 当前时间的处理
	// var newDate = new Date();
	var yyyy = withData(date.getFullYear()),
		MM = withData(date.getMonth() + 1),
		dd = withData(date.getDate()),
		d = yyyy + "/" + MM + "/" + dd
	return d;
}
//获取该月的第一天yyyy/MM/dd格式
function getFirstDay(date) {
	var yyyy = withData(date.getFullYear()),
		MM = withData(date.getMonth() + 1),
		d = yyyy + "/" + MM + "/" + "01"
	return d;
}
//获取该月第一天yyyy-MM-dd格式
function getfirstDay(date) {
	var yyyy = withData(date.getFullYear()),
		MM = withData(date.getMonth() + 1),
		d = yyyy + "-" + MM + "-" + 1
	return d;
}
//获取该月的最大天数（也是最后一天）
function mGetDate(year, month) {
	var d = new Date(year, month, 0);
	return d.getDate();
}

function getLastDate(date) {
	var y = withData(date.getFullYear()),
		M = withData(date.getMonth() + 1),
		d = mGetDate(y, M),
		last = y + "/" + M + "/" + d
	return last;
}

// //根据yyyy-MM-dd格式的时间去获取MM值
// function getMonth(date){
//   var month=new Array(),
//   month=date.split('-'),

// }

function dateTimePicker(startYear, endYear, date) {
	// 返回默认显示的数组和联动数组的声明
	var dateTime = [],
		dateTimeArray = [
			[],
			[],
			[]
		];
	var start = startYear || 1978;
	var end = endYear || 2100;
	// 默认开始显示数据
	//console.log(date) 初始无数据
	var defaultDate = date ? [...date.split(' ')[0].split('-'), ...date.split(' ')[1].split(':')] : getNewDateArry();
	// console.log(defaultDate[2]) day
	// 处理联动列表数据
	/*年月日 时分秒*/
	dateTimeArray[0] = getLoopArray(start, end, '年');
	dateTimeArray[1] = getLoopArray(1, 12, '月');
	dateTimeArray[2] = getMonthDay(defaultDate[0], defaultDate[1]);
	// dateTimeArray[3] = getLoopArray(0, 23);
	// dateTimeArray[4] = getLoopArray(0, 59);

	dateTimeArray.forEach((current, index) => {
		dateTime.push(current.indexOf(defaultDate[index]));
	});

	return {
		dateTimeArray: dateTimeArray,
		defaultDay: defaultDate[2],
		dateTime: dateTime
	}
}

function getTime() {
	// 获取现在的时间 time
	var d = new Date();
	var hour = d.getHours(),
		minu = d.getMinutes();
	return withData(hour) + ":" + withData(minu);
}

function dateFormat(fmt, date) {
	let ret;
	var newDate = new Date(date);
	const opt = {
		"Y+": newDate.getFullYear().toString(), // 年
		"m+": (newDate.getMonth() + 1).toString(), // 月
		"d+": newDate.getDate().toString(), // 日
		"H+": newDate.getHours().toString(), // 时
		"M+": newDate.getMinutes().toString(), // 分
		"S+": newDate.getSeconds().toString() // 秒
		// 有其他格式化字符需求可以继续添加，必须转化成字符串
	};
	for (let k in opt) {
		ret = new RegExp("(" + k + ")").exec(fmt);
		if (ret) {
			fmt = fmt.replace(
				ret[1],
				ret[1].length == 1 ? opt[k] : opt[k].padStart(ret[1].length, "0")
			);
		}
	}
	return fmt;
}

/**
 * 距今多少天
 * @param {} date 
 */
function agoDays(date) {
	const timestamp = new Date().getTime(); // 当前日期时间戳
	const newDate = new Date(date).getTime(); // 传入时间的时间戳
	let daysNum = 0;
	if (newDate > timestamp) {
		daysNum = parseInt((newDate - timestamp) / (1000 * 60 * 60 * 24)); //天数
	}
	return daysNum;
}

/**
 * 时间格式化兼容ISO
 * @param {*} dateObj 
 * @param {*} fmt 
 */
function dateFormatObj(dateObj, fmt) {
	let date = strToDate(dateObj)
	var o = {
		"M+": date.getMonth() + 1, //月份         
		"d+": date.getDate(), //日         
		"h+": date.getHours() % 12 == 0 ? 12 : date.getHours() % 12, //小时         
		"H+": date.getHours(), //小时         
		"m+": date.getMinutes(), //分         
		"s+": date.getSeconds(), //秒         
		"q+": Math.floor((date.getMonth() + 3) / 3), //季度         
		"S": date.getMilliseconds() //毫秒         
	};
	var week = {
		"0": "日",
		"1": "一",
		"2": "二",
		"3": "三",
		"4": "四",
		"5": "五",
		"6": "六"
	};
	if (/(y+)/.test(fmt)) {
		fmt = fmt.replace(RegExp.$1, (date.getFullYear().toString() + "").substr(4 - RegExp.$1.length));
	}
	if (/(E+)/.test(fmt)) {
		fmt = fmt.replace(RegExp.$1, ((RegExp.$1.length > 1) ? (RegExp.$1.length > 2 ? "星期" : "周") : "") + week[date
			.getDay().toString() + ""]);
	}
	for (var k in o) {
		if (new RegExp("(" + k + ")").test(fmt)) {
			fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k].toString()) : (("00" + o[k].toString()).substr(("" +
				o[k].toString()).length)));
		}
	}
	return fmt;
}

/**
 * 字符串转换为时间
 * @param  {String} src 字符串
 */
function strToDate(dateObj) {
	dateObj = dateObj.replace(/T/g, ' ').replace(/\.[\d]{3}Z/, '').replace(/(-)/g, '/')
	if (dateObj.indexOf(".") > 0) dateObj = dateObj.slice(0, dateObj.indexOf("."))
	return new Date(dateObj)
}

module.exports = {
	dateTimePicker: dateTimePicker,
	getMonthDay: getMonthDay,
	getTime: getTime,
	getWeekday: getWeekday,
	getNowDate: getNowDate,
	getNewDate: getNewDate,
	getFirstDay: getFirstDay,
	getLastDate: getLastDate,
	getfirstDay: getfirstDay,
	dateFormat: dateFormat,
	dateFormatObj: dateFormatObj,
	agoDays: agoDays
}